<!DOCTYPE html>
<!-- saved from url=(0014)about:internet -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="x-ua-compatible" content="IE=9" >

<title>chapter13: Miscellaneous Graphs</title>

<style type="text/css">
body, td {
   font-family: sans-serif;
   background-color: white;
   font-size: 12px;
   margin: 8px;
}

tt, code, pre {
   font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}

h1 { 
   font-size:2.2em; 
}

h2 { 
   font-size:1.8em; 
}

h3 { 
   font-size:1.4em; 
}

h4 { 
   font-size:1.0em; 
}

h5 { 
   font-size:0.9em; 
}

h6 { 
   font-size:0.8em; 
}

a:visited {
   color: rgb(50%, 0%, 50%);
}

pre {	
   margin-top: 0;
   max-width: 95%;
   border: 1px solid #ccc;
   white-space: pre-wrap;
}

pre code {
   display: block; padding: 0.5em;
}

code.r, code.cpp {
   background-color: #F8F8F8;
}

table, td, th {
  border: none;
}

blockquote {
   color:#666666;
   margin:0;
   padding-left: 1em;
   border-left: 0.5em #EEE solid;
}

hr {
   height: 0px;
   border-bottom: none;
   border-top-width: thin;
   border-top-style: dotted;
   border-top-color: #999999;
}

@media print {
   * { 
      background: transparent !important; 
      color: black !important; 
      filter:none !important; 
      -ms-filter: none !important; 
   }

   body { 
      font-size:12pt; 
      max-width:100%; 
   }
       
   a, a:visited { 
      text-decoration: underline; 
   }

   hr { 
      visibility: hidden;
      page-break-before: always;
   }

   pre, blockquote { 
      padding-right: 1em; 
      page-break-inside: avoid; 
   }

   tr, img { 
      page-break-inside: avoid; 
   }

   img { 
      max-width: 100% !important; 
   }

   @page :left { 
      margin: 15mm 20mm 15mm 10mm; 
   }
     
   @page :right { 
      margin: 15mm 10mm 15mm 20mm; 
   }

   p, h2, h3 { 
      orphans: 3; widows: 3; 
   }

   h2, h3 { 
      page-break-after: avoid; 
   }
}

</style>

<!-- Styles for R syntax highlighter -->
<style type="text/css">
   pre .operator,
   pre .paren {
     color: rgb(104, 118, 135)
   }

   pre .literal {
     color: rgb(88, 72, 246)
   }

   pre .number {
     color: rgb(0, 0, 205);
   }

   pre .comment {
     color: rgb(76, 136, 107);
   }

   pre .keyword {
     color: rgb(0, 0, 255);
   }

   pre .identifier {
     color: rgb(0, 0, 0);
   }

   pre .string {
     color: rgb(3, 106, 7);
   }
</style>

<!-- R syntax highlighter -->
<script type="text/javascript">
var hljs=new function(){function m(p){return p.replace(/&/gm,"&amp;").replace(/</gm,"&lt;")}function f(r,q,p){return RegExp(q,"m"+(r.cI?"i":"")+(p?"g":""))}function b(r){for(var p=0;p<r.childNodes.length;p++){var q=r.childNodes[p];if(q.nodeName=="CODE"){return q}if(!(q.nodeType==3&&q.nodeValue.match(/\s+/))){break}}}function h(t,s){var p="";for(var r=0;r<t.childNodes.length;r++){if(t.childNodes[r].nodeType==3){var q=t.childNodes[r].nodeValue;if(s){q=q.replace(/\n/g,"")}p+=q}else{if(t.childNodes[r].nodeName=="BR"){p+="\n"}else{p+=h(t.childNodes[r])}}}if(/MSIE [678]/.test(navigator.userAgent)){p=p.replace(/\r/g,"\n")}return p}function a(s){var r=s.className.split(/\s+/);r=r.concat(s.parentNode.className.split(/\s+/));for(var q=0;q<r.length;q++){var p=r[q].replace(/^language-/,"");if(e[p]){return p}}}function c(q){var p=[];(function(s,t){for(var r=0;r<s.childNodes.length;r++){if(s.childNodes[r].nodeType==3){t+=s.childNodes[r].nodeValue.length}else{if(s.childNodes[r].nodeName=="BR"){t+=1}else{if(s.childNodes[r].nodeType==1){p.push({event:"start",offset:t,node:s.childNodes[r]});t=arguments.callee(s.childNodes[r],t);p.push({event:"stop",offset:t,node:s.childNodes[r]})}}}}return t})(q,0);return p}function k(y,w,x){var q=0;var z="";var s=[];function u(){if(y.length&&w.length){if(y[0].offset!=w[0].offset){return(y[0].offset<w[0].offset)?y:w}else{return w[0].event=="start"?y:w}}else{return y.length?y:w}}function t(D){var A="<"+D.nodeName.toLowerCase();for(var B=0;B<D.attributes.length;B++){var C=D.attributes[B];A+=" "+C.nodeName.toLowerCase();if(C.value!==undefined&&C.value!==false&&C.value!==null){A+='="'+m(C.value)+'"'}}return A+">"}while(y.length||w.length){var v=u().splice(0,1)[0];z+=m(x.substr(q,v.offset-q));q=v.offset;if(v.event=="start"){z+=t(v.node);s.push(v.node)}else{if(v.event=="stop"){var p,r=s.length;do{r--;p=s[r];z+=("</"+p.nodeName.toLowerCase()+">")}while(p!=v.node);s.splice(r,1);while(r<s.length){z+=t(s[r]);r++}}}}return z+m(x.substr(q))}function j(){function q(x,y,v){if(x.compiled){return}var u;var s=[];if(x.k){x.lR=f(y,x.l||hljs.IR,true);for(var w in x.k){if(!x.k.hasOwnProperty(w)){continue}if(x.k[w] instanceof Object){u=x.k[w]}else{u=x.k;w="keyword"}for(var r in u){if(!u.hasOwnProperty(r)){continue}x.k[r]=[w,u[r]];s.push(r)}}}if(!v){if(x.bWK){x.b="\\b("+s.join("|")+")\\s"}x.bR=f(y,x.b?x.b:"\\B|\\b");if(!x.e&&!x.eW){x.e="\\B|\\b"}if(x.e){x.eR=f(y,x.e)}}if(x.i){x.iR=f(y,x.i)}if(x.r===undefined){x.r=1}if(!x.c){x.c=[]}x.compiled=true;for(var t=0;t<x.c.length;t++){if(x.c[t]=="self"){x.c[t]=x}q(x.c[t],y,false)}if(x.starts){q(x.starts,y,false)}}for(var p in e){if(!e.hasOwnProperty(p)){continue}q(e[p].dM,e[p],true)}}function d(B,C){if(!j.called){j();j.called=true}function q(r,M){for(var L=0;L<M.c.length;L++){if((M.c[L].bR.exec(r)||[null])[0]==r){return M.c[L]}}}function v(L,r){if(D[L].e&&D[L].eR.test(r)){return 1}if(D[L].eW){var M=v(L-1,r);return M?M+1:0}return 0}function w(r,L){return L.i&&L.iR.test(r)}function K(N,O){var M=[];for(var L=0;L<N.c.length;L++){M.push(N.c[L].b)}var r=D.length-1;do{if(D[r].e){M.push(D[r].e)}r--}while(D[r+1].eW);if(N.i){M.push(N.i)}return f(O,M.join("|"),true)}function p(M,L){var N=D[D.length-1];if(!N.t){N.t=K(N,E)}N.t.lastIndex=L;var r=N.t.exec(M);return r?[M.substr(L,r.index-L),r[0],false]:[M.substr(L),"",true]}function z(N,r){var L=E.cI?r[0].toLowerCase():r[0];var M=N.k[L];if(M&&M instanceof Array){return M}return false}function F(L,P){L=m(L);if(!P.k){return L}var r="";var O=0;P.lR.lastIndex=0;var M=P.lR.exec(L);while(M){r+=L.substr(O,M.index-O);var N=z(P,M);if(N){x+=N[1];r+='<span class="'+N[0]+'">'+M[0]+"</span>"}else{r+=M[0]}O=P.lR.lastIndex;M=P.lR.exec(L)}return r+L.substr(O,L.length-O)}function J(L,M){if(M.sL&&e[M.sL]){var r=d(M.sL,L);x+=r.keyword_count;return r.value}else{return F(L,M)}}function I(M,r){var L=M.cN?'<span class="'+M.cN+'">':"";if(M.rB){y+=L;M.buffer=""}else{if(M.eB){y+=m(r)+L;M.buffer=""}else{y+=L;M.buffer=r}}D.push(M);A+=M.r}function G(N,M,Q){var R=D[D.length-1];if(Q){y+=J(R.buffer+N,R);return false}var P=q(M,R);if(P){y+=J(R.buffer+N,R);I(P,M);return P.rB}var L=v(D.length-1,M);if(L){var O=R.cN?"</span>":"";if(R.rE){y+=J(R.buffer+N,R)+O}else{if(R.eE){y+=J(R.buffer+N,R)+O+m(M)}else{y+=J(R.buffer+N+M,R)+O}}while(L>1){O=D[D.length-2].cN?"</span>":"";y+=O;L--;D.length--}var r=D[D.length-1];D.length--;D[D.length-1].buffer="";if(r.starts){I(r.starts,"")}return R.rE}if(w(M,R)){throw"Illegal"}}var E=e[B];var D=[E.dM];var A=0;var x=0;var y="";try{var s,u=0;E.dM.buffer="";do{s=p(C,u);var t=G(s[0],s[1],s[2]);u+=s[0].length;if(!t){u+=s[1].length}}while(!s[2]);if(D.length>1){throw"Illegal"}return{r:A,keyword_count:x,value:y}}catch(H){if(H=="Illegal"){return{r:0,keyword_count:0,value:m(C)}}else{throw H}}}function g(t){var p={keyword_count:0,r:0,value:m(t)};var r=p;for(var q in e){if(!e.hasOwnProperty(q)){continue}var s=d(q,t);s.language=q;if(s.keyword_count+s.r>r.keyword_count+r.r){r=s}if(s.keyword_count+s.r>p.keyword_count+p.r){r=p;p=s}}if(r.language){p.second_best=r}return p}function i(r,q,p){if(q){r=r.replace(/^((<[^>]+>|\t)+)/gm,function(t,w,v,u){return w.replace(/\t/g,q)})}if(p){r=r.replace(/\n/g,"<br>")}return r}function n(t,w,r){var x=h(t,r);var v=a(t);var y,s;if(v){y=d(v,x)}else{return}var q=c(t);if(q.length){s=document.createElement("pre");s.innerHTML=y.value;y.value=k(q,c(s),x)}y.value=i(y.value,w,r);var u=t.className;if(!u.match("(\\s|^)(language-)?"+v+"(\\s|$)")){u=u?(u+" "+v):v}if(/MSIE [678]/.test(navigator.userAgent)&&t.tagName=="CODE"&&t.parentNode.tagName=="PRE"){s=t.parentNode;var p=document.createElement("div");p.innerHTML="<pre><code>"+y.value+"</code></pre>";t=p.firstChild.firstChild;p.firstChild.cN=s.cN;s.parentNode.replaceChild(p.firstChild,s)}else{t.innerHTML=y.value}t.className=u;t.result={language:v,kw:y.keyword_count,re:y.r};if(y.second_best){t.second_best={language:y.second_best.language,kw:y.second_best.keyword_count,re:y.second_best.r}}}function o(){if(o.called){return}o.called=true;var r=document.getElementsByTagName("pre");for(var p=0;p<r.length;p++){var q=b(r[p]);if(q){n(q,hljs.tabReplace)}}}function l(){if(window.addEventListener){window.addEventListener("DOMContentLoaded",o,false);window.addEventListener("load",o,false)}else{if(window.attachEvent){window.attachEvent("onload",o)}else{window.onload=o}}}var e={};this.LANGUAGES=e;this.highlight=d;this.highlightAuto=g;this.fixMarkup=i;this.highlightBlock=n;this.initHighlighting=o;this.initHighlightingOnLoad=l;this.IR="[a-zA-Z][a-zA-Z0-9_]*";this.UIR="[a-zA-Z_][a-zA-Z0-9_]*";this.NR="\\b\\d+(\\.\\d+)?";this.CNR="\\b(0[xX][a-fA-F0-9]+|(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)";this.BNR="\\b(0b[01]+)";this.RSR="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|\\.|-|-=|/|/=|:|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~";this.ER="(?![\\s\\S])";this.BE={b:"\\\\.",r:0};this.ASM={cN:"string",b:"'",e:"'",i:"\\n",c:[this.BE],r:0};this.QSM={cN:"string",b:'"',e:'"',i:"\\n",c:[this.BE],r:0};this.CLCM={cN:"comment",b:"//",e:"$"};this.CBLCLM={cN:"comment",b:"/\\*",e:"\\*/"};this.HCM={cN:"comment",b:"#",e:"$"};this.NM={cN:"number",b:this.NR,r:0};this.CNM={cN:"number",b:this.CNR,r:0};this.BNM={cN:"number",b:this.BNR,r:0};this.inherit=function(r,s){var p={};for(var q in r){p[q]=r[q]}if(s){for(var q in s){p[q]=s[q]}}return p}}();hljs.LANGUAGES.cpp=function(){var a={keyword:{"false":1,"int":1,"float":1,"while":1,"private":1,"char":1,"catch":1,"export":1,virtual:1,operator:2,sizeof:2,dynamic_cast:2,typedef:2,const_cast:2,"const":1,struct:1,"for":1,static_cast:2,union:1,namespace:1,unsigned:1,"long":1,"throw":1,"volatile":2,"static":1,"protected":1,bool:1,template:1,mutable:1,"if":1,"public":1,friend:2,"do":1,"return":1,"goto":1,auto:1,"void":2,"enum":1,"else":1,"break":1,"new":1,extern:1,using:1,"true":1,"class":1,asm:1,"case":1,typeid:1,"short":1,reinterpret_cast:2,"default":1,"double":1,register:1,explicit:1,signed:1,typename:1,"try":1,"this":1,"switch":1,"continue":1,wchar_t:1,inline:1,"delete":1,alignof:1,char16_t:1,char32_t:1,constexpr:1,decltype:1,noexcept:1,nullptr:1,static_assert:1,thread_local:1,restrict:1,_Bool:1,complex:1},built_in:{std:1,string:1,cin:1,cout:1,cerr:1,clog:1,stringstream:1,istringstream:1,ostringstream:1,auto_ptr:1,deque:1,list:1,queue:1,stack:1,vector:1,map:1,set:1,bitset:1,multiset:1,multimap:1,unordered_set:1,unordered_map:1,unordered_multiset:1,unordered_multimap:1,array:1,shared_ptr:1}};return{dM:{k:a,i:"</",c:[hljs.CLCM,hljs.CBLCLM,hljs.QSM,{cN:"string",b:"'\\\\?.",e:"'",i:"."},{cN:"number",b:"\\b(\\d+(\\.\\d*)?|\\.\\d+)(u|U|l|L|ul|UL|f|F)"},hljs.CNM,{cN:"preprocessor",b:"#",e:"$"},{cN:"stl_container",b:"\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<",e:">",k:a,r:10,c:["self"]}]}}}();hljs.LANGUAGES.r={dM:{c:[hljs.HCM,{cN:"number",b:"\\b0[xX][0-9a-fA-F]+[Li]?\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\b\\d+(?:[eE][+\\-]?\\d*)?L\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\b\\d+\\.(?!\\d)(?:i\\b)?",e:hljs.IMMEDIATE_RE,r:1},{cN:"number",b:"\\b\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d*)?i?\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\.\\d+(?:[eE][+\\-]?\\d*)?i?\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"keyword",b:"(?:tryCatch|library|setGeneric|setGroupGeneric)\\b",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\.\\.\\.",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\.\\.\\d+(?![\\w.])",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\b(?:function)",e:hljs.IMMEDIATE_RE,r:2},{cN:"keyword",b:"(?:if|in|break|next|repeat|else|for|return|switch|while|try|stop|warning|require|attach|detach|source|setMethod|setClass)\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"literal",b:"(?:NA|NA_integer_|NA_real_|NA_character_|NA_complex_)\\b",e:hljs.IMMEDIATE_RE,r:10},{cN:"literal",b:"(?:NULL|TRUE|FALSE|T|F|Inf|NaN)\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"identifier",b:"[a-zA-Z.][a-zA-Z0-9._]*\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"operator",b:"<\\-(?!\\s*\\d)",e:hljs.IMMEDIATE_RE,r:2},{cN:"operator",b:"\\->|<\\-",e:hljs.IMMEDIATE_RE,r:1},{cN:"operator",b:"%%|~",e:hljs.IMMEDIATE_RE},{cN:"operator",b:">=|<=|==|!=|\\|\\||&&|=|\\+|\\-|\\*|/|\\^|>|<|!|&|\\||\\$|:",e:hljs.IMMEDIATE_RE,r:0},{cN:"operator",b:"%",e:"%",i:"\\n",r:1},{cN:"identifier",b:"`",e:"`",r:0},{cN:"string",b:'"',e:'"',c:[hljs.BE],r:0},{cN:"string",b:"'",e:"'",c:[hljs.BE],r:0},{cN:"paren",b:"[[({\\])}]",e:hljs.IMMEDIATE_RE,r:0}]}};
hljs.initHighlightingOnLoad();
</script>




</head>

<body>
<h1>chapter13: Miscellaneous Graphs</h1>

<h2>13.1.Making a Correlation Matrix</h2>

<pre><code class="r">library(ggplot2)
head(mtcars)
</code></pre>

<pre><code>##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
</code></pre>

<pre><code class="r">
mcor &lt;- cor(mtcars)

# Print mcor and round to 2 digits
round(mcor, digits = 2)
</code></pre>

<pre><code>##        mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
## mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
## cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
## disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
## hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
## drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
## wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
## qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00  0.74 -0.23 -0.21 -0.66
## vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74  1.00  0.17  0.21 -0.57
## am    0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17  1.00  0.79  0.06
## gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79  1.00  0.27
## carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27  1.00
</code></pre>

<pre><code class="r">
# install.packages(&#39;corrplot&#39;)
library(corrplot)

corrplot(mcor)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-1"/> </p>

<pre><code class="r">
corrplot(mcor, method = &quot;shade&quot;, shade.col = NA, tl.col = &quot;black&quot;, tl.srt = 45)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-1"/> </p>

<pre><code class="r">

# Generate a lighter palette
col &lt;- colorRampPalette(c(&quot;#BB4444&quot;, &quot;#EE9988&quot;, &quot;#FFFFFF&quot;, &quot;#77AADD&quot;, &quot;#4477AA&quot;))

corrplot(mcor, method = &quot;shade&quot;, shade.col = NA, tl.col = &quot;black&quot;, tl.srt = 45, 
    col = col(200), addCoef.col = &quot;black&quot;, addcolorlabel = &quot;no&quot;, order = &quot;AOE&quot;)
</code></pre>

<pre><code>## Warning: &quot;addcolorlabel&quot; is not a graphical parameter
## Warning: &quot;addcolorlabel&quot; is not a graphical parameter
## Warning: &quot;addcolorlabel&quot; is not a graphical parameter
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-1"/> </p>

<h2>13.2.Plotting a Functioon</h2>

<pre><code class="r"># The data frame is only used for setting the range
p &lt;- ggplot(data.frame(x = c(-3, 3)), aes(x = x))

p + stat_function(fun = dnorm)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-2"/> </p>

<pre><code class="r">
p + stat_function(fun = dt, args = list(df = 2))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-2"/> </p>

<pre><code class="r">
myfun &lt;- function(xvar) {
    1/(1 + exp(-xvar + 10))
}

ggplot(data.frame(x = c(0, 20)), aes(x = x)) + stat_function(fun = myfun)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-2"/> </p>

<h2>13.3.Shading a Subregion Under a Function Curve</h2>

<pre><code class="r"># Return dnorm(x) for 0 &lt; x &lt; 2, and NA for all other x
dnorm_limit &lt;- function(x) {
    y &lt;- dnorm(x)
    y[x &lt; 0 | x &gt; 2] &lt;- NA
    return(y)
}

# ggplot() with dummy data
p &lt;- ggplot(data.frame(x = c(-3, 3)), aes(x = x))

p + stat_function(fun = dnorm_limit, geom = &quot;area&quot;, fill = &quot;blue&quot;, alpha = 0.2) + 
    stat_function(fun = dnorm)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-3"/> </p>

<pre><code class="r">
limitRange &lt;- function(fun, min, max) {
    function(x) {
        y &lt;- fun(x)
        y[x &lt; min | x &gt; max] &lt;- NA
        return(y)
    }
}

# This returns a function
dlimit &lt;- limitRange(dnorm, 0, 2)

# Now we&#39;ll try out the new function -- it only returns values for inputs
# between 0 and 2
dlimit(-2:4)
</code></pre>

<pre><code>## [1]      NA      NA 0.39894 0.24197 0.05399      NA      NA
</code></pre>

<pre><code class="r">
p + stat_function(fun = dnorm) + stat_function(fun = limitRange(dnorm, 0, 2), 
    geom = &quot;area&quot;, fill = &quot;blue&quot;, alpha = 0.2)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-3"/> </p>

<h2>13.4.Creating a Network Graph</h2>

<pre><code class="r"># May need to install first, with install.packages(&#39;igraph&#39;)
# install.packages(&#39;igraph&#39;)
library(igraph)

# Specify edges for a directed graph
gd &lt;- graph(c(1, 2, 2, 3, 2, 4, 1, 4, 5, 5, 3, 6))
plot(gd)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-4"/> </p>

<pre><code class="r">
# For an undirected graph
gu &lt;- graph(c(1, 2, 2, 3, 2, 4, 1, 4, 5, 5, 3, 6), directed = FALSE)
# No labels
plot(gu, vertex.label = NA)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-4"/> </p>

<pre><code class="r">
str(gd)
</code></pre>

<pre><code>## IGRAPH D--- 6 6 -- 
## + edges:
## [1] 1-&gt;2 2-&gt;3 2-&gt;4 1-&gt;4 5-&gt;5 3-&gt;6
</code></pre>

<pre><code class="r">
str(gu)
</code></pre>

<pre><code>## IGRAPH U--- 6 6 -- 
## + edges:
## [1] 1--2 2--3 2--4 1--4 5--5 3--6
</code></pre>

<pre><code class="r">
set.seed(229)
plot(gu)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-4"/> </p>

<pre><code class="r">
library(gcookbook)  # For the data set
head(madmen2)
</code></pre>

<pre><code>##                  Name1        Name2
## 1          Abe Drexler  Peggy Olson
## 2              Allison   Don Draper
## 3          Arthur Case Betty Draper
## 4 Bellhop in Baltimore   Sal Romano
## 5     Bethany Van Nuys   Don Draper
## 6         Betty Draper   Don Draper
</code></pre>

<pre><code class="r">
# Create a graph object from the data set
g &lt;- graph.data.frame(madmen2, directed = TRUE)

# Remove unnecessary margins
par(mar = c(0, 0, 0, 0))

plot(g, layout = layout.fruchterman.reingold, vertex.size = 8, edge.arrow.size = 0.5, 
    vertex.label = NA)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-4"/> </p>

<pre><code class="r">
g &lt;- graph.data.frame(madmen, directed = FALSE)
par(mar = c(0, 0, 0, 0))  # Remove unnecessary margins
plot(g, layout = layout.circle, vertex.size = 8, vertex.label = NA)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-4"/> </p>

<h2>13.5.Using Text Labels in a Network Graph</h2>

<pre><code class="r">library(igraph)
library(gcookbook) # For the data set
# Copy madmen and drop every other row
m &lt;- madmen[1:nrow(madmen) %% 2 == 1, ]
g &lt;- graph.data.frame(m, directed=FALSE)

# Print out the names of each vertex
V(g)$name
</code></pre>

<pre><code>##  [1] &quot;Betty Draper&quot;      &quot;Don Draper&quot;        &quot;Harry Crane&quot;      
##  [4] &quot;Joan Holloway&quot;     &quot;Lane Pryce&quot;        &quot;Peggy Olson&quot;      
##  [7] &quot;Pete Campbell&quot;     &quot;Roger Sterling&quot;    &quot;Sal Romano&quot;       
## [10] &quot;Henry Francis&quot;     &quot;Allison&quot;           &quot;Candace&quot;          
## [13] &quot;Faye Miller&quot;       &quot;Megan Calvet&quot;      &quot;Rachel Menken&quot;    
## [16] &quot;Suzanne Farrell&quot;   &quot;Hildy&quot;             &quot;Franklin&quot;         
## [19] &quot;Rebecca Pryce&quot;     &quot;Abe Drexler&quot;       &quot;Duck Phillips&quot;    
## [22] &quot;Playtex bra model&quot; &quot;Ida Blankenship&quot;   &quot;Mirabelle Ames&quot;   
## [25] &quot;Vicky&quot;             &quot;Kitty Romano&quot;
</code></pre>

<pre><code class="r">

plot(g, layout=layout.fruchterman.reingold,
     vertex.size        = 4,          # Smaller nodes
     vertex.label       = V(g)$name,  # Set the labels
     vertex.label.cex   = 0.8,        # Slightly smaller font
     vertex.label.dist  = 0.4,        # Offset the labels
     vertex.label.color = &quot;black&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-5"/> </p>

<pre><code class="r">
# This is equivalent to the preceding code
V(g)$size        &lt;- 4
V(g)$label       &lt;- V(g)$name
V(g)$label.cex   &lt;- 0.8
V(g)$label.dist  &lt;- 0.4
V(g)$label.color &lt;- &quot;black&quot;

# Set a property of the entire graph
g$layout &lt;- layout.fruchterman.reingold

plot(g)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-5"/> </p>

<pre><code class="r">

# View the edges
E(g)
</code></pre>

<pre><code>## Edge sequence:
##                                            
## [1]  Henry Francis     -- Betty Draper     
## [2]  Allison           -- Don Draper       
## [3]  Don Draper        -- Betty Draper     
## [4]  Candace           -- Don Draper       
## [5]  Faye Miller       -- Don Draper       
## [6]  Megan Calvet      -- Don Draper       
## [7]  Rachel Menken     -- Don Draper       
## [8]  Suzanne Farrell   -- Don Draper       
## [9]  Hildy             -- Harry Crane      
## [10] Franklin          -- Joan Holloway    
## [11] Roger Sterling    -- Joan Holloway    
## [12] Rebecca Pryce     -- Lane Pryce       
## [13] Abe Drexler       -- Peggy Olson      
## [14] Duck Phillips     -- Peggy Olson      
## [15] Pete Campbell     -- Peggy Olson      
## [16] Playtex bra model -- Pete Campbell    
## [17] Ida Blankenship   -- Roger Sterling   
## [18] Mirabelle Ames    -- Roger Sterling   
## [19] Vicky             -- Roger Sterling   
## [20] Kitty Romano      -- Sal Romano
</code></pre>

<pre><code class="r">
# Set some of the labels to &quot;M&quot;
E(g)[c(2,11,19)]$label &lt;- &quot;M&quot;

# Set color of all to grey, and then color a few red
E(g)$color             &lt;- &quot;grey70&quot;
E(g)[c(2,11,19)]$color &lt;- &quot;red&quot;

plot(g)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-5"/> </p>

<h2>13.6.Creating a Heat Map</h2>

<pre><code class="r">head(presidents)
</code></pre>

<pre><code>## [1] NA 87 82 75 63 50
</code></pre>

<pre><code class="r">
str(presidents)
</code></pre>

<pre><code>##  Time-Series [1:120] from 1945 to 1975: NA 87 82 75 63 50 43 32 35 60 ...
</code></pre>

<pre><code class="r">
pres_rating &lt;- data.frame(rating = as.numeric(presidents), year = as.numeric(floor(time(presidents))), 
    quarter = as.numeric(cycle(presidents)))
head(pres_rating)
</code></pre>

<pre><code>##   rating year quarter
## 1     NA 1945       1
## 2     87 1945       2
## 3     82 1945       3
## 4     75 1945       4
## 5     63 1946       1
## 6     50 1946       2
</code></pre>

<pre><code class="r">
# Base plot
p &lt;- ggplot(pres_rating, aes(x = year, y = quarter, fill = rating))

# Using geom_tile()
p + geom_tile()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-6"/> </p>

<pre><code class="r">
# Using geom_raster() - looks the same, but a little more efficient
p + geom_raster()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-6"/> </p>

<pre><code class="r">
p + geom_tile() + scale_x_continuous(breaks = seq(1940, 1976, by = 4)) + scale_y_reverse() + 
    scale_fill_gradient2(midpoint = 50, mid = &quot;grey70&quot;, limits = c(0, 100))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-6"/> </p>

<h2>13.7.Creating a Three-Dimensional Scatter Plot</h2>

<pre><code class="r"># You may need to install first, with install.packages(&quot;rgl&quot;)
library(rgl)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type=&quot;s&quot;, size=0.75, lit=FALSE)

# Function to interleave the elements of two vectors
interleave &lt;- function(v1, v2)  as.vector(rbind(v1,v2))

# Plot the points
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,
       xlab=&quot;Weight&quot;, ylab=&quot;Displacement&quot;, zlab=&quot;MPG&quot;,
       size=.75, type=&quot;s&quot;, lit=FALSE)

# Add the segments
segments3d(interleave(mtcars$wt,   mtcars$wt),
           interleave(mtcars$disp, mtcars$disp),
           interleave(mtcars$mpg,  min(mtcars$mpg)),
           alpha=0.4, col=&quot;blue&quot;)


# Make plot without axis ticks or labels
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,
       xlab = &quot;&quot;, ylab = &quot;&quot;, zlab = &quot;&quot;,
       axes = FALSE,
       size=.75, type=&quot;s&quot;, lit=FALSE)

segments3d(interleave(mtcars$wt,   mtcars$wt),
           interleave(mtcars$disp, mtcars$disp),
           interleave(mtcars$mpg,  min(mtcars$mpg)),
           alpha = 0.4, col = &quot;blue&quot;)

# Draw the box.
rgl.bbox(color=&quot;grey50&quot;,          # grey60 surface and black text
         emission=&quot;grey50&quot;,       # emission color is grey50
         xlen=0, ylen=0, zlen=0)  # Don&#39;t add tick marks

# Set default color of future objects to black
rgl.material(color=&quot;black&quot;)

# Add axes to specific sides. Possible values are &quot;x--&quot;, &quot;x-+&quot;, &quot;x+-&quot;, and &quot;x++&quot;.
axes3d(edges=c(&quot;x--&quot;, &quot;y+-&quot;, &quot;z--&quot;),
       ntick=6,                       # Attempt 6 tick marks on each side
       cex=.75)                       # Smaller font

# Add axis labels. &#39;line&#39; specifies how far to set the label from the axis.
mtext3d(&quot;Weight&quot;,       edge=&quot;x--&quot;, line=2)
mtext3d(&quot;Displacement&quot;, edge=&quot;y+-&quot;, line=3)
mtext3d(&quot;MPG&quot;,          edge=&quot;z--&quot;, line=3)
</code></pre>

<h2>13.8.Adding a Prediction Surface to a Three-Dimensional Plot</h2>

<pre><code class="r"># Given a model, predict zvar from xvar and yvar
# Defaults to range of x and y variables, and a 16x16 grid
predictgrid &lt;- function(model, xvar, yvar, zvar, res = 16, type = NULL) {
  # Find the range of the predictor variable. This works for lm and glm
  # and some others, but may require customization for others.
  xrange &lt;- range(model$model[[xvar]])
  yrange &lt;- range(model$model[[yvar]])

  newdata &lt;- expand.grid(x = seq(xrange[1], xrange[2], length.out = res),
                         y = seq(yrange[1], yrange[2], length.out = res))
  names(newdata) &lt;- c(xvar, yvar)
  newdata[[zvar]] &lt;- predict(model, newdata = newdata, type = type)
  newdata
}


# Convert long-style data frame with x, y, and z vars into a list
# with x and y as row/column values, and z as a matrix.
df2mat &lt;- function(p, xvar = NULL, yvar = NULL, zvar = NULL) {
  if (is.null(xvar)) xvar &lt;- names(p)[1]
  if (is.null(yvar)) yvar &lt;- names(p)[2]
  if (is.null(zvar)) zvar &lt;- names(p)[3]

  x &lt;- unique(p[[xvar]])
  y &lt;- unique(p[[yvar]])
  z &lt;- matrix(p[[zvar]], nrow = length(y), ncol = length(x))

  m &lt;- list(x, y, z)
  names(m) &lt;- c(xvar, yvar, zvar)
  m
}

# Function to interleave the elements of two vectors
interleave &lt;- function(v1, v2)  as.vector(rbind(v1,v2))


#install.packages(&quot;rgl&quot;)
library(rgl)

# Make a copy of the data set
m &lt;- mtcars

# Generate a linear model
mod &lt;- lm(mpg ~ wt + disp + wt:disp, data = m)

# Get predicted values of mpg from wt and disp
m$pred_mpg &lt;- predict(mod)

# Get predicted mpg from a grid of wt and disp
mpgrid_df &lt;- predictgrid(mod, &quot;wt&quot;, &quot;disp&quot;, &quot;mpg&quot;)
mpgrid_list &lt;- df2mat(mpgrid_df)

# Make the plot with the data points
plot3d(m$wt, m$disp, m$mpg, type=&quot;s&quot;, size=0.5, lit=FALSE)

# Add the corresponding predicted points (smaller)
spheres3d(m$wt, m$disp, m$pred_mpg, alpha=0.4, type=&quot;s&quot;, size=0.5, lit=FALSE)

# Add line segments showing the error
segments3d(interleave(m$wt,   m$wt),
           interleave(m$disp, m$disp),
           interleave(m$mpg,  m$pred_mpg),
           alpha=0.4, col=&quot;red&quot;)

# Add the mesh of predicted values
surface3d(mpgrid_list$wt, mpgrid_list$disp, mpgrid_list$mpg,
          alpha=0.4, front=&quot;lines&quot;, back=&quot;lines&quot;)


plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,
       xlab = &quot;&quot;, ylab = &quot;&quot;, zlab = &quot;&quot;,
       axes = FALSE,
       size=.5, type=&quot;s&quot;, lit=FALSE)

# Add the corresponding predicted points (smaller)
spheres3d(m$wt, m$disp, m$pred_mpg, alpha=0.4, type=&quot;s&quot;, size=0.5, lit=FALSE)

# Add line segments showing the error
segments3d(interleave(m$wt,   m$wt),
           interleave(m$disp, m$disp),
           interleave(m$mpg,  m$pred_mpg),
           alpha=0.4, col=&quot;red&quot;)

# Add the mesh of predicted values
surface3d(mpgrid_list$wt, mpgrid_list$disp, mpgrid_list$mpg,
          alpha=0.4, front=&quot;lines&quot;, back=&quot;lines&quot;)

# Draw the box
rgl.bbox(color=&quot;grey50&quot;,          # grey60 surface and black text
         emission=&quot;grey50&quot;,       # emission color is grey50
         xlen=0, ylen=0, zlen=0)  # Don&#39;t add tick marks

# Set default color of future objects to black
rgl.material(color=&quot;black&quot;)

# Add axes to specific sides. Possible values are &quot;x--&quot;, &quot;x-+&quot;, &quot;x+-&quot;, and &quot;x++&quot;.
axes3d(edges=c(&quot;x--&quot;, &quot;y+-&quot;, &quot;z--&quot;),
       ntick=6,                       # Attempt 6 tick marks on each side
       cex=.75)                       # Smaller font

# Add axis labels. &#39;line&#39; specifies how far to set the label from the axis.
mtext3d(&quot;Weight&quot;,       edge=&quot;x--&quot;, line=2)
mtext3d(&quot;Displacement&quot;, edge=&quot;y+-&quot;, line=3)
mtext3d(&quot;MPG&quot;,          edge=&quot;z--&quot;, line=3)
</code></pre>

<h2>13.9.Saving a Three-Dimensional Plot</h2>

<pre><code class="r">library(rgl)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type = &quot;s&quot;, size = 0.75, lit = FALSE)

rgl.snapshot(&quot;3dplot.png&quot;, fmt = &quot;png&quot;)


rgl.postscript(&quot;figs/miscgraph/3dplot.pdf&quot;, fmt = &quot;pdf&quot;)

rgl.postscript(&quot;figs/miscgraph/3dplot.ps&quot;, fmt = &quot;ps&quot;)


# Save the current viewpoint
view &lt;- par3d(&quot;userMatrix&quot;)

# Restore the saved viewpoint
par3d(userMatrix = view)


dput(view)
</code></pre>

<pre><code>## structure(c(1, 0, 0, 0, 0, 0.342020143325668, -0.939692620785909, 
## 0, 0, 0.939692620785909, 0.342020143325668, 0, 0, 0, 0, 1), .Dim = c(4L, 
## 4L))
</code></pre>

<pre><code class="r">
structure(c(0.907931625843048, 0.267511069774628, -0.322642296552658, 0, -0.410978674888611, 
    0.417272746562958, -0.810543060302734, 0, -0.0821993798017502, 0.868516683578491, 
    0.488796472549438, 0, 0, 0, 0, 1), .Dim = c(4L, 4L))
</code></pre>

<pre><code>##         [,1]    [,2]    [,3] [,4]
## [1,]  0.9079 -0.4110 -0.0822    0
## [2,]  0.2675  0.4173  0.8685    0
## [3,] -0.3226 -0.8105  0.4888    0
## [4,]  0.0000  0.0000  0.0000    1
</code></pre>

<pre><code class="r">


view &lt;- structure(c(0.907931625843048, 0.267511069774628, -0.322642296552658, 
    0, -0.410978674888611, 0.417272746562958, -0.810543060302734, 0, -0.0821993798017502, 
    0.868516683578491, 0.488796472549438, 0, 0, 0, 0, 1), .Dim = c(4L, 4L))

par3d(userMatrix = view)
</code></pre>

<h2>13.10.Animating a Three-Dimensional Plot</h2>

<pre><code class="r">library(rgl)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type = &quot;s&quot;, size = 0.75, lit = FALSE)

play3d(spin3d())
</code></pre>

<pre><code>## Error: no device opened with id1
</code></pre>

<pre><code class="r">
# Spin on x-axis, at 4 rpm, for 20 seconds
play3d(spin3d(axis = c(1, 0, 0), rpm = 4), duration = 20)
</code></pre>

<pre><code>## Error: no device opened with id2
</code></pre>

<pre><code class="r">
# Spin on z axis, at 4 rpm, for 15 seconds
movie3d(spin3d(axis = c(0, 0, 1), rpm = 4), duration = 15, fps = 50)
</code></pre>

<pre><code>## Writing movie000.png 
Writing movie001.png 
Writing movie002.png 
Writing movie003.png 
Writing movie004.png 
Writing movie005.png 
Writing movie006.png 
Writing movie007.png 
Writing movie008.png 
Writing movie009.png 
Writing movie010.png 
Writing movie011.png 
Writing movie012.png 
Writing movie013.png 
Writing movie014.png 
Writing movie015.png 
Writing movie016.png 
Writing movie017.png 
Writing movie018.png 
Writing movie019.png 
Writing movie020.png 
Writing movie021.png 
Writing movie022.png 
Writing movie023.png 
Writing movie024.png 
Writing movie025.png 
Writing movie026.png 
Writing movie027.png 
Writing movie028.png 
Writing movie029.png 
Writing movie030.png 
Writing movie031.png 
Writing movie032.png 
Writing movie033.png 
Writing movie034.png 
Writing movie035.png 
Writing movie036.png 
Writing movie037.png 
Writing movie038.png 
Writing movie039.png 
Writing movie040.png 
Writing movie041.png 
Writing movie042.png 
Writing movie043.png 
Writing movie044.png 
Writing movie045.png 
Writing movie046.png 
Writing movie047.png 
Writing movie048.png 
Writing movie049.png 
Writing movie050.png 
Writing movie051.png 
Writing movie052.png 
Writing movie053.png 
Writing movie054.png 
Writing movie055.png 
Writing movie056.png 
Writing movie057.png 
Writing movie058.png 
Writing movie059.png 
Writing movie060.png 
Writing movie061.png 
Writing movie062.png 
Writing movie063.png 
Writing movie064.png 
Writing movie065.png 
Writing movie066.png 
Writing movie067.png 
Writing movie068.png 
Writing movie069.png 
Writing movie070.png 
Writing movie071.png 
Writing movie072.png 
Writing movie073.png 
Writing movie074.png 
Writing movie075.png 
Writing movie076.png 
Writing movie077.png 
Writing movie078.png 
Writing movie079.png 
Writing movie080.png 
Writing movie081.png 
Writing movie082.png 
Writing movie083.png 
Writing movie084.png 
Writing movie085.png 
Writing movie086.png 
Writing movie087.png 
Writing movie088.png 
Writing movie089.png 
Writing movie090.png 
Writing movie091.png 
Writing movie092.png 
Writing movie093.png 
Writing movie094.png 
Writing movie095.png 
Writing movie096.png 
Writing movie097.png 
Writing movie098.png 
Writing movie099.png 
Writing movie100.png 
Writing movie101.png 
Writing movie102.png 
Writing movie103.png 
Writing movie104.png 
Writing movie105.png 
Writing movie106.png 
Writing movie107.png 
Writing movie108.png 
Writing movie109.png 
Writing movie110.png 
Writing movie111.png 
Writing movie112.png 
Writing movie113.png 
Writing movie114.png 
Writing movie115.png 
Writing movie116.png 
Writing movie117.png 
Writing movie118.png 
Writing movie119.png 
Writing movie120.png 
Writing movie121.png 
Writing movie122.png 
Writing movie123.png 
Writing movie124.png 
Writing movie125.png 
Writing movie126.png 
Writing movie127.png 
Writing movie128.png 
Writing movie129.png 
Writing movie130.png 
Writing movie131.png 
Writing movie132.png 
Writing movie133.png 
</code></pre>

<pre><code>## Error: no device opened with id3
</code></pre>

<h2>13.11.Creating a Dendrogram</h2>

<pre><code class="r">library(gcookbook)  # For the data set

# Get data from year 2009
c2 &lt;- subset(countries, Year == 2009)

# Drop rows that have any NA values
c2 &lt;- c2[complete.cases(c2), ]

# Pick out a random 25 countries (Set random seed to make this repeatable)
set.seed(201)
c2 &lt;- c2[sample(1:nrow(c2), 25), ]

c2
</code></pre>

<pre><code>##                   Name Code Year     GDP laborrate healthexp infmortality
## 6731          Mongolia  MNG 2009  1690.4      72.9     74.20         27.8
## 1733            Canada  CAN 2009 39599.0      67.8   4379.76          5.2
## 4028         Guatemala  GTM 2009  2685.0      66.9    186.12         25.9
## 611            Austria  AUT 2009 45555.4      60.4   5037.31          3.6
## 10964           Zambia  ZMB 2009  1006.4      69.2     47.06         71.5
## 1478          Bulgaria  BGR 2009  6403.1      54.5    474.85         11.1
## 662         Azerbaijan  AZE 2009  4808.2      63.0    284.73         41.1
## 3824            Greece  GRC 2009 28936.5      53.7   3040.73          3.5
## 1070             Benin  BEN 2009   771.7      72.7     31.93         74.7
## 2957  Egypt, Arab Rep.  EGY 2009  2370.7      48.8    113.30         20.0
## 4844             Italy  ITA 2009 35073.3      49.1   3327.63          3.2
## 7037             Nepal  NPL 2009   438.2      71.5     25.34         43.3
## 6119          Malaysia  MYS 2009  6908.7      62.0    336.44          5.6
## 4793            Israel  ISR 2009 26102.4      57.1   1966.47          3.7
## 5252       Korea, Rep.  KOR 2009 17110.0      60.9   1107.95          4.3
## 5099             Kenya  KEN 2009   744.4      82.2     33.25         56.3
## 152            Algeria  DZA 2009  4022.2      58.5    267.95         32.0
## 2447           Croatia  HRV 2009 14322.6      53.0   1120.37          4.9
## 5609           Lesotho  LSO 2009   800.4      74.0     70.05         67.0
## 4691           Ireland  IRL 2009 49737.9      63.6   4951.84          3.4
## 7343           Nigeria  NGA 2009  1091.1      56.2     69.30         90.4
## 5660           Liberia  LBR 2009   229.3      71.1     29.36         77.6
## 5558           Lebanon  LBN 2009  8321.4      46.1    663.27         19.4
## 5966    Macedonia, FYR  MKD 2009  4510.2      54.0    313.69         10.6
## 10148     Turkmenistan  TKM 2009  3710.5      68.0     77.07         48.0
</code></pre>

<pre><code class="r">
rownames(c2) &lt;- c2$Name
c2 &lt;- c2[, 4:7]
c2
</code></pre>

<pre><code>##                      GDP laborrate healthexp infmortality
## Mongolia          1690.4      72.9     74.20         27.8
## Canada           39599.0      67.8   4379.76          5.2
## Guatemala         2685.0      66.9    186.12         25.9
## Austria          45555.4      60.4   5037.31          3.6
## Zambia            1006.4      69.2     47.06         71.5
## Bulgaria          6403.1      54.5    474.85         11.1
## Azerbaijan        4808.2      63.0    284.73         41.1
## Greece           28936.5      53.7   3040.73          3.5
## Benin              771.7      72.7     31.93         74.7
## Egypt, Arab Rep.  2370.7      48.8    113.30         20.0
## Italy            35073.3      49.1   3327.63          3.2
## Nepal              438.2      71.5     25.34         43.3
## Malaysia          6908.7      62.0    336.44          5.6
## Israel           26102.4      57.1   1966.47          3.7
## Korea, Rep.      17110.0      60.9   1107.95          4.3
## Kenya              744.4      82.2     33.25         56.3
## Algeria           4022.2      58.5    267.95         32.0
## Croatia          14322.6      53.0   1120.37          4.9
## Lesotho            800.4      74.0     70.05         67.0
## Ireland          49737.9      63.6   4951.84          3.4
## Nigeria           1091.1      56.2     69.30         90.4
## Liberia            229.3      71.1     29.36         77.6
## Lebanon           8321.4      46.1    663.27         19.4
## Macedonia, FYR    4510.2      54.0    313.69         10.6
## Turkmenistan      3710.5      68.0     77.07         48.0
</code></pre>

<pre><code class="r">
c3 &lt;- scale(c2)
c3
</code></pre>

<pre><code>##                      GDP laborrate  healthexp infmortality
## Mongolia         -0.6783   1.15029 -0.6341394     -0.08335
## Canada            1.7505   0.59747  1.9736220     -0.88015
## Guatemala        -0.6146   0.49992 -0.5663495     -0.15033
## Austria           2.1321  -0.20465  2.3718821     -0.93656
## Zambia           -0.7222   0.74923 -0.6505785      1.45737
## Bulgaria         -0.3764  -0.84418 -0.3914778     -0.67213
## Azerbaijan       -0.4786   0.07718 -0.5066289      0.38557
## Greece            1.0673  -0.93090  1.1626100     -0.94009
## Benin            -0.7372   1.12861 -0.6597408      1.57019
## Egypt, Arab Rep. -0.6348  -1.46203 -0.6104582     -0.35835
## Italy             1.4605  -1.42951  1.3363751     -0.95066
## Nepal            -0.7586   0.99853 -0.6637287      0.46313
## Malaysia         -0.3440  -0.03122 -0.4753076     -0.86605
## Israel            0.8857  -0.56235  0.5119590     -0.93303
## Korea, Rep.       0.3096  -0.15045 -0.0080252     -0.91188
## Kenya            -0.7390   2.15836 -0.6589411      0.92147
## Algeria          -0.5289  -0.41060 -0.5167913      0.06473
## Croatia           0.1310  -1.00677 -0.0005011     -0.89073
## Lesotho          -0.7354   1.26952 -0.6366519      1.29872
## Ireland           2.4001   0.14221  2.3201175     -0.94361
## Nigeria          -0.7167  -0.65991 -0.6371077      2.12372
## Liberia          -0.7720   0.95518 -0.6612990      1.67244
## Lebanon          -0.2535  -1.75470 -0.2773526     -0.37950
## Macedonia, FYR   -0.4977  -0.89838 -0.4890859     -0.68976
## Turkmenistan     -0.5489   0.61915 -0.6324003      0.62884
## attr(,&quot;scaled:center&quot;)
##          GDP    laborrate    healthexp infmortality 
##     12277.96        62.29      1121.20        30.16 
## attr(,&quot;scaled:scale&quot;)
##          GDP    laborrate    healthexp infmortality 
##    15607.853        9.226     1651.057       28.363
</code></pre>

<pre><code class="r">
attr(, &quot;scaled:scale&quot;)
</code></pre>

<pre><code>## Error: 1元素是空的
</code></pre>

<pre><code class="r">
hc &lt;- hclust(dist(c3))

# Make the dendrogram
plot(hc)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-11"/> </p>

<pre><code class="r">
# With text aligned
plot(hc, hang = -1)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-11"/> </p>

<h2>13.12.Creating a Vector Field</h2>

<pre><code class="r">library(gcookbook)  # For the data set
head(isabel)
</code></pre>

<pre><code>##     x     y     z vx vy vz  t speed
## 1 -83 41.70 0.035 NA NA NA NA    NA
## 2 -83 41.56 0.035 NA NA NA NA    NA
## 3 -83 41.41 0.035 NA NA NA NA    NA
## 4 -83 41.27 0.035 NA NA NA NA    NA
## 5 -83 41.12 0.035 NA NA NA NA    NA
## 6 -83 40.98 0.035 NA NA NA NA    NA
</code></pre>

<pre><code class="r">
islice &lt;- subset(isabel, z == min(z))

ggplot(islice, aes(x = x, y = y)) + geom_segment(aes(xend = x + vx/50, yend = y + 
    vy/50), size = 0.25)  # Make the line segments 0.25 mm thick
</code></pre>

<pre><code>## Warning: Removed 3745 rows containing missing values (geom_segment).
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-12"/> </p>

<pre><code class="r">

# ==================================== Take a slice where z is equal to the
# minimum value of z
islice &lt;- subset(isabel, z == min(z))

# Keep 1 out of every &#39;by&#39; values in vector x
every_n &lt;- function(x, by = 2) {
    x &lt;- sort(x)
    x[seq(1, length(x), by = by)]
}

# Keep 1 of every 4 values in x and y
keepx &lt;- every_n(unique(isabel$x), by = 4)
keepy &lt;- every_n(unique(isabel$y), by = 4)

# Keep only those rows where x value is in keepx and y value is in keepy
islicesub &lt;- subset(islice, x %in% keepx &amp; y %in% keepy)


# Need to load grid for arrow() function
library(grid)

# Make the plot with the subset, and use an arrowhead 0.1 cm long
ggplot(islicesub, aes(x = x, y = y)) + geom_segment(aes(xend = x + vx/50, yend = y + 
    vy/50), arrow = arrow(length = unit(0.1, &quot;cm&quot;)), size = 0.25)
</code></pre>

<pre><code>## Warning: Removed 248 rows containing missing values (geom_segment).
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-12"/> </p>

<pre><code class="r">

# The existing &#39;speed&#39; column includes the z component. We&#39;ll calculate
# speedxy, the horizontal speed.
islicesub$speedxy &lt;- sqrt(islicesub$vx^2 + islicesub$vy^2)

# Map speed to alpha
ggplot(islicesub, aes(x = x, y = y)) + geom_segment(aes(xend = x + vx/50, yend = y + 
    vy/50, alpha = speed), arrow = arrow(length = unit(0.1, &quot;cm&quot;)), size = 0.6)
</code></pre>

<pre><code>## Warning: Removed 248 rows containing missing values (geom_segment).
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-12"/> </p>

<pre><code class="r">

# Get USA map data
usa &lt;- map_data(&quot;usa&quot;)

# Map speed to colour, and set go from &#39;grey80&#39; to &#39;darkred&#39;
ggplot(islicesub, aes(x = x, y = y)) + geom_segment(aes(xend = x + vx/50, yend = y + 
    vy/50, colour = speed), arrow = arrow(length = unit(0.1, &quot;cm&quot;)), size = 0.6) + 
    scale_colour_continuous(low = &quot;grey80&quot;, high = &quot;darkred&quot;) + geom_path(aes(x = long, 
    y = lat, group = group), data = usa) + coord_cartesian(xlim = range(islicesub$x), 
    ylim = range(islicesub$y))
</code></pre>

<pre><code>## Warning: Removed 248 rows containing missing values (geom_segment).
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-12"/> </p>

<pre><code class="r">

# Keep 1 out of every 5 values in x and y, and 1 in 2 values in z
keepx &lt;- every_n(unique(isabel$x), by = 5)
keepy &lt;- every_n(unique(isabel$y), by = 5)
keepz &lt;- every_n(unique(isabel$z), by = 2)

isub &lt;- subset(isabel, x %in% keepx &amp; y %in% keepy &amp; z %in% keepz)

ggplot(isub, aes(x = x, y = y)) + geom_segment(aes(xend = x + vx/50, yend = y + 
    vy/50, colour = speed), arrow = arrow(length = unit(0.1, &quot;cm&quot;)), size = 0.5) + 
    scale_colour_continuous(low = &quot;grey80&quot;, high = &quot;darkred&quot;) + facet_wrap(~z)
</code></pre>

<pre><code>## Warning: Removed 151 rows containing missing values (geom_segment).
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-12"/> </p>

<h2>13.13.Creating a QQ Plot</h2>

<pre><code class="r">library(gcookbook)  # For the data set

# QQ plot of height
qqnorm(heightweight$heightIn)
qqline(heightweight$heightIn)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-13"/> </p>

<pre><code class="r">
# QQ plot of age
qqnorm(heightweight$ageYear)
qqline(heightweight$ageYear)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-13"/> </p>

<h2>13.14.Creating a Graph of an Empirical Cumulative Distribution Function</h2>

<pre><code class="r">library(gcookbook)  # For the data set

# ecdf of heightIn
ggplot(heightweight, aes(x = heightIn)) + stat_ecdf()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-14"/> </p>

<pre><code class="r">
# ecdf of ageYear
ggplot(heightweight, aes(x = ageYear)) + stat_ecdf()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-14"/> </p>

<h2>13.15.Creating a Mosaic Plot</h2>

<pre><code class="r">UCBAdmissions
</code></pre>

<pre><code>## , , Dept = A
## 
##           Gender
## Admit      Male Female
##   Admitted  512     89
##   Rejected  313     19
## 
## , , Dept = B
## 
##           Gender
## Admit      Male Female
##   Admitted  353     17
##   Rejected  207      8
## 
## , , Dept = C
## 
##           Gender
## Admit      Male Female
##   Admitted  120    202
##   Rejected  205    391
## 
## , , Dept = D
## 
##           Gender
## Admit      Male Female
##   Admitted  138    131
##   Rejected  279    244
## 
## , , Dept = E
## 
##           Gender
## Admit      Male Female
##   Admitted   53     94
##   Rejected  138    299
## 
## , , Dept = F
## 
##           Gender
## Admit      Male Female
##   Admitted   22     24
##   Rejected  351    317
</code></pre>

<pre><code class="r">
# Print a &#39;flat&#39; contingency table
ftable(UCBAdmissions)
</code></pre>

<pre><code>##                 Dept   A   B   C   D   E   F
## Admit    Gender                             
## Admitted Male        512 353 120 138  53  22
##          Female       89  17 202 131  94  24
## Rejected Male        313 207 205 279 138 351
##          Female       19   8 391 244 299 317
</code></pre>

<pre><code class="r">
dimnames(UCBAdmissions)
</code></pre>

<pre><code>## $Admit
## [1] &quot;Admitted&quot; &quot;Rejected&quot;
## 
## $Gender
## [1] &quot;Male&quot;   &quot;Female&quot;
## 
## $Dept
## [1] &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;F&quot;
</code></pre>

<pre><code class="r">
# You may need to install first, with install.packages(&#39;vcd&#39;)
library(vcd)
# Split by Admit, then Gender, then Dept
mosaic(~Admit + Gender + Dept, data = UCBAdmissions)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-15"/> </p>

<pre><code class="r">

mosaic(~Dept + Gender + Admit, data = UCBAdmissions, highlighting = &quot;Admit&quot;, 
    highlighting_fill = c(&quot;lightblue&quot;, &quot;pink&quot;), direction = c(&quot;v&quot;, &quot;h&quot;, &quot;v&quot;))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-15"/> </p>

<pre><code class="r">

# Another possible set of splitting directions
mosaic(~Dept + Gender + Admit, data = UCBAdmissions, highlighting = &quot;Admit&quot;, 
    highlighting_fill = c(&quot;lightblue&quot;, &quot;pink&quot;), direction = c(&quot;v&quot;, &quot;v&quot;, &quot;h&quot;))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-15"/> </p>

<pre><code class="r">
# This order makes it difficult to compare male and female
mosaic(~Dept + Gender + Admit, data = UCBAdmissions, highlighting = &quot;Admit&quot;, 
    highlighting_fill = c(&quot;lightblue&quot;, &quot;pink&quot;), direction = c(&quot;v&quot;, &quot;h&quot;, &quot;h&quot;))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-15"/> </p>

<h2>13.16.Creating a Pie Chart</h2>

<pre><code class="r">library(MASS)  # For the data set

# Get a table of how many cases are in each level of fold
fold &lt;- table(survey$Fold)
fold
</code></pre>

<pre><code>## 
##  L on R Neither  R on L 
##      99      18     120
</code></pre>

<pre><code class="r">
# Make the pie chart
pie(fold)

pie(c(99, 18, 120), labels = c(&quot;L on R&quot;, &quot;Neither&quot;, &quot;R on L&quot;))
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-16"/> </p>

<h2>13.17.Creating a Map</h2>

<pre><code class="r">library(maps)  # For map data
# Get map data for USA
states_map &lt;- map_data(&quot;state&quot;)

ggplot(states_map, aes(x = long, y = lat, group = group)) + geom_polygon(fill = &quot;white&quot;, 
    colour = &quot;black&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-17"/> </p>

<pre><code class="r">
# geom_path (no fill) and Mercator projection
ggplot(states_map, aes(x = long, y = lat, group = group)) + geom_path() + coord_map(&quot;mercator&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-17"/> </p>

<pre><code class="r">
# Get map data for world
world_map &lt;- map_data(&quot;world&quot;)
head(world_map)
</code></pre>

<pre><code>##     long   lat group order region subregion
## 1 -133.4 58.42     1     1 Canada      &lt;NA&gt;
## 2 -132.3 57.16     1     2 Canada      &lt;NA&gt;
## 3 -132.0 56.99     1     3 Canada      &lt;NA&gt;
## 4 -131.9 56.74     1     4 Canada      &lt;NA&gt;
## 5 -130.2 56.10     1     5 Canada      &lt;NA&gt;
## 6 -130.0 55.91     1     6 Canada      &lt;NA&gt;
</code></pre>

<pre><code class="r">
head(sort(unique(world_map$region)))
</code></pre>

<pre><code>## [1] &quot;Afghanistan&quot;     &quot;Albania&quot;         &quot;Algeria&quot;         &quot;American Samoa&quot; 
## [5] &quot;Andaman Islands&quot; &quot;Andorra&quot;
</code></pre>

<pre><code class="r">
# You might have noticed that it&#39;s a little out of date!

east_asia &lt;- map_data(&quot;world&quot;, region = c(&quot;Japan&quot;, &quot;China&quot;, &quot;North Korea&quot;, &quot;South Korea&quot;))
# Map region to fill color
ggplot(east_asia, aes(x = long, y = lat, group = group, fill = region)) + geom_polygon(colour = &quot;black&quot;) + 
    scale_fill_brewer(palette = &quot;Set2&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-17"/> </p>

<pre><code class="r">

# Get New Zealand data from world map
nz1 &lt;- map_data(&quot;world&quot;, region = &quot;New Zealand&quot;)
nz1 &lt;- subset(nz1, long &gt; 0 &amp; lat &gt; -48)  # Trim off islands
ggplot(nz1, aes(x = long, y = lat, group = group)) + geom_path()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-17"/> </p>

<pre><code class="r">
# Get New Zealand data from the nz map
nz2 &lt;- map_data(&quot;nz&quot;)
ggplot(nz2, aes(x = long, y = lat, group = group)) + geom_path()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-17"/> </p>

<h2>13.18.Creating a Choropleth Map</h2>

<pre><code class="r"># Transform the USArrests data set to the correct format
crimes &lt;- data.frame(state = tolower(rownames(USArrests)), USArrests)
head(crimes)
</code></pre>

<pre><code>##                 state Murder Assault UrbanPop Rape
## Alabama       alabama   13.2     236       58 21.2
## Alaska         alaska   10.0     263       48 44.5
## Arizona       arizona    8.1     294       80 31.0
## Arkansas     arkansas    8.8     190       50 19.5
## California california    9.0     276       91 40.6
## Colorado     colorado    7.9     204       78 38.7
</code></pre>

<pre><code class="r">
# install.packages(&#39;maps&#39;)
library(maps)  # For map data
states_map &lt;- map_data(&quot;state&quot;)

# Merge the data sets together
crime_map &lt;- merge(states_map, crimes, by.x = &quot;region&quot;, by.y = &quot;state&quot;)

# After merging, the order has changed, which would lead to polygons drawn
# in the incorrect order. So, we sort the data.
head(crime_map)
</code></pre>

<pre><code>##    region   long   lat group order subregion Murder Assault UrbanPop Rape
## 1 alabama -87.46 30.39     1     1      &lt;NA&gt;   13.2     236       58 21.2
## 2 alabama -87.48 30.37     1     2      &lt;NA&gt;   13.2     236       58 21.2
## 3 alabama -87.95 30.25     1    13      &lt;NA&gt;   13.2     236       58 21.2
## 4 alabama -88.01 30.24     1    14      &lt;NA&gt;   13.2     236       58 21.2
## 5 alabama -88.02 30.25     1    15      &lt;NA&gt;   13.2     236       58 21.2
## 6 alabama -87.53 30.37     1     3      &lt;NA&gt;   13.2     236       58 21.2
</code></pre>

<pre><code class="r">

library(plyr)  # For arrange() function
# Sort by group, then order
crime_map &lt;- arrange(crime_map, group, order)
head(crime_map)
</code></pre>

<pre><code>##    region   long   lat group order subregion Murder Assault UrbanPop Rape
## 1 alabama -87.46 30.39     1     1      &lt;NA&gt;   13.2     236       58 21.2
## 2 alabama -87.48 30.37     1     2      &lt;NA&gt;   13.2     236       58 21.2
## 3 alabama -87.53 30.37     1     3      &lt;NA&gt;   13.2     236       58 21.2
## 4 alabama -87.53 30.33     1     4      &lt;NA&gt;   13.2     236       58 21.2
## 5 alabama -87.57 30.33     1     5      &lt;NA&gt;   13.2     236       58 21.2
## 6 alabama -87.59 30.33     1     6      &lt;NA&gt;   13.2     236       58 21.2
</code></pre>

<pre><code class="r">
ggplot(crime_map, aes(x = long, y = lat, group = group, fill = Assault)) + geom_polygon(colour = &quot;black&quot;) + 
    coord_map(&quot;polyconic&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-18"/> </p>

<pre><code class="r">

ggplot(crimes, aes(map_id = state, fill = Assault)) + geom_map(map = states_map, 
    colour = &quot;black&quot;) + scale_fill_gradient2(low = &quot;#559999&quot;, mid = &quot;grey90&quot;, 
    high = &quot;#BB650B&quot;, midpoint = median(crimes$Assault)) + expand_limits(x = states_map$long, 
    y = states_map$lat) + coord_map(&quot;polyconic&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-18"/> </p>

<pre><code class="r">

# Find the quantile bounds
qa &lt;- quantile(crimes$Assault, c(0, 0.2, 0.4, 0.6, 0.8, 1))
qa
</code></pre>

<pre><code>##    0%   20%   40%   60%   80%  100% 
##  45.0  98.8 135.0 188.8 254.2 337.0
</code></pre>

<pre><code class="r">
# Add a column of the quantile category
crimes$Assault_q &lt;- cut(crimes$Assault, qa, labels = c(&quot;0-20%&quot;, &quot;20-40%&quot;, &quot;40-60%&quot;, 
    &quot;60-80%&quot;, &quot;80-100%&quot;), include.lowest = TRUE)
head(crimes)
</code></pre>

<pre><code>##                 state Murder Assault UrbanPop Rape Assault_q
## Alabama       alabama   13.2     236       58 21.2    60-80%
## Alaska         alaska   10.0     263       48 44.5   80-100%
## Arizona       arizona    8.1     294       80 31.0   80-100%
## Arkansas     arkansas    8.8     190       50 19.5    60-80%
## California california    9.0     276       91 40.6   80-100%
## Colorado     colorado    7.9     204       78 38.7    60-80%
</code></pre>

<pre><code class="r">
# Generate a discrete color palette with 5 values
pal &lt;- colorRampPalette(c(&quot;#559999&quot;, &quot;grey80&quot;, &quot;#BB650B&quot;))(5)
pal
</code></pre>

<pre><code>## [1] &quot;#559999&quot; &quot;#90B2B2&quot; &quot;#CCCCCC&quot; &quot;#C3986B&quot; &quot;#BB650B&quot;
</code></pre>

<pre><code class="r">
ggplot(crimes, aes(map_id = state, fill = Assault_q)) + geom_map(map = states_map, 
    colour = &quot;black&quot;) + scale_fill_manual(values = pal) + expand_limits(x = states_map$long, 
    y = states_map$lat) + coord_map(&quot;polyconic&quot;) + labs(fill = &quot;Assault Rate\nPercentile&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-18"/> </p>

<pre><code class="r">


# The &#39;state&#39; column in the crimes data is to be matched to the &#39;region&#39;
# column in the states_map data
ggplot(crimes, aes(map_id = state, fill = Assault)) + geom_map(map = states_map) + 
    expand_limits(x = states_map$long, y = states_map$lat) + coord_map(&quot;polyconic&quot;)
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-18"/> </p>

<h2>13.19.Making a Map with a Clean Background</h2>

<pre><code class="r"># Create a theme with many of the background elements removed
theme_clean &lt;- function(base_size = 12) {
    require(grid)  # Needed for unit() function
    theme_grey(base_size) %+replace% theme(axis.title = element_blank(), axis.text = element_blank(), 
        panel.background = element_blank(), panel.grid = element_blank(), axis.ticks.length = unit(0, 
            &quot;cm&quot;), axis.ticks.margin = unit(0, &quot;cm&quot;), panel.margin = unit(0, 
            &quot;lines&quot;), plot.margin = unit(c(0, 0, 0, 0), &quot;lines&quot;), complete = TRUE)
}

ggplot(crimes, aes(map_id = state, fill = Assault_q)) + geom_map(map = states_map, 
    colour = &quot;black&quot;) + scale_fill_manual(values = pal) + expand_limits(x = states_map$long, 
    y = states_map$lat) + coord_map(&quot;polyconic&quot;) + labs(fill = &quot;Assault Rate\nPercentile&quot;) + 
    theme_clean()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-19"/> </p>

<h2>13.20.Creating a Map from a Shapefile</h2>

<pre><code class="r"># install.packages(&#39;maptools&#39;) install.packages(&#39;sp&#39;) 首先要安装这两个包
library(maptools)
</code></pre>

<pre><code>## Loading required package: sp
## Checking rgeos availability: TRUE
</code></pre>

<pre><code class="r">gpclibPermit()
</code></pre>

<pre><code>## [1] FALSE
</code></pre>

<pre><code class="r">
# 地图包从这里下载：http://www.gadm.org/
# 记住要把几个TWN_adm2的文件都放在TWN_adm文件夹下 仅仅放一个shp会报错 Load
# the shapefile and convert to a data frame
taiwan_shp &lt;- readShapePoly(&quot;TWN_adm/TWN_adm2.shp&quot;)
taiwan_map &lt;- fortify(taiwan_shp)
</code></pre>

<pre><code>## Regions defined for each Polygons
</code></pre>

<pre><code class="r">
ggplot(taiwan_map, aes(x = long, y = lat, group = group)) + geom_path()
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-20"/> </p>

<pre><code class="r">

taiwan_shp &lt;- readShapePoly(&quot;TWN_adm/TWN_adm2.shp&quot;)

# Look at the structure of the object
head(str(taiwan_shp))
</code></pre>

<pre><code>## Formal class &#39;SpatialPolygonsDataFrame&#39; [package &quot;sp&quot;] with 5 slots
##   ..@ data       :&#39;data.frame&#39;:  22 obs. of  11 variables:
##   .. ..$ ID_0     : int [1:22] 223 223 223 223 223 223 223 223 223 223 ...
##   .. ..$ ISO      : Factor w/ 1 level &quot;TWN&quot;: 1 1 1 1 1 1 1 1 1 1 ...
##   .. ..$ NAME_0   : Factor w/ 1 level &quot;Taiwan&quot;: 1 1 1 1 1 1 1 1 1 1 ...
##   .. ..$ ID_1     : int [1:22] 1 2 3 4 4 4 4 4 4 4 ...
##   .. ..$ NAME_1   : Factor w/ 4 levels &quot;Kaohsiung&quot;,&quot;Pratas Islands&quot;,..: 1 2 3 4 4 4 4 4 4 4 ...
##   .. ..$ ID_2     : int [1:22] 1 2 3 4 5 6 7 8 9 10 ...
##   .. ..$ NAME_2   : Factor w/ 21 levels &quot;Changhwa&quot;,&quot;Chiayi&quot;,..: 7 NA 18 1 2 3 4 5 6 8 ...
##   .. ..$ NL_NAME_2: Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##   .. ..$ VARNAME_2: Factor w/ 16 levels &quot;Gaoxiong&quot;,&quot;Gaoxiong Shi&quot;,..: 2 NA 8 16 4 13 3 14 1 5 ...
##   .. ..$ TYPE_2   : Factor w/ 3 levels &quot;Chuan-shih&quot;,&quot;District|Hsien&quot;,..: 1 NA 1 2 2 2 2 2 2 3 ...
##   .. ..$ ENGTYPE_2: Factor w/ 3 levels &quot;County&quot;,&quot;Municipality&quot;,..: 3 NA 3 1 1 1 1 1 1 2 ...
##   .. ..- attr(*, &quot;data_types&quot;)= chr [1:11] &quot;N&quot; &quot;C&quot; &quot;C&quot; &quot;N&quot; ...
##   ..@ polygons   :List of 22
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.3 22.7
##   .. .. .. .. .. .. ..@ area   : num 0.00602
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:827, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 22.8
##   .. .. .. .. .. .. ..@ area   : num 1.8e-08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 120.2 120.2 120.2 120.2 22.8 ...
##   .. .. .. ..@ plotOrder: int [1:2] 1 2
##   .. .. .. ..@ labpt    : num [1:2] 120.3 22.7
##   .. .. .. ..@ ID       : chr &quot;0&quot;
##   .. .. .. ..@ area     : num 0.00602
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 116.7 20.7
##   .. .. .. .. .. .. ..@ area   : num 0.000123
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:189, 1:2] 117 117 117 117 117 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 116.7 20.7
##   .. .. .. ..@ ID       : chr &quot;1&quot;
##   .. .. .. ..@ area     : num 0.000123
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 25
##   .. .. .. .. .. .. ..@ area   : num 0.0238
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:14, 1:2] 122 122 122 122 122 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 122 25
##   .. .. .. ..@ ID       : chr &quot;2&quot;
##   .. .. .. ..@ area     : num 0.0238
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 4
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.4 24.1
##   .. .. .. .. .. .. ..@ area   : num 0.00115
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:262, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.5 23.9
##   .. .. .. .. .. .. ..@ area   : num 0.104
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3022, 1:2] 120 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.5 24.2
##   .. .. .. .. .. .. ..@ area   : num 3.85e-08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 120.5 120.5 120.5 120.5 24.2 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.5 24.2
##   .. .. .. .. .. .. ..@ area   : num 3.98e-08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 120.5 120.5 120.5 120.5 24.2 ...
##   .. .. .. ..@ plotOrder: int [1:4] 2 1 4 3
##   .. .. .. ..@ labpt    : num [1:2] 120.5 23.9
##   .. .. .. ..@ ID       : chr &quot;3&quot;
##   .. .. .. ..@ area     : num 0.106
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 5
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 23.3
##   .. .. .. .. .. .. ..@ area   : num 2.72e-08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 120.2 120.2 120.2 120.2 23.3 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.1 23.3
##   .. .. .. .. .. .. ..@ area   : num 7.17e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:87, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 23.4
##   .. .. .. .. .. .. ..@ area   : num 2.82e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:73, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 23.4
##   .. .. .. .. .. .. ..@ area   : num 1.26e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:37, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 0.152
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2246, 1:2] 120 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:5] 5 2 3 4 1
##   .. .. .. ..@ labpt    : num [1:2] 120.5 23.4
##   .. .. .. ..@ ID       : chr &quot;4&quot;
##   .. .. .. ..@ area     : num 0.152
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.9 24.8
##   .. .. .. .. .. .. ..@ area   : num 2.63e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:19, 1:2] 121 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.1 24.6
##   .. .. .. .. .. .. ..@ area   : num 0.156
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1382, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:2] 2 1
##   .. .. .. ..@ labpt    : num [1:2] 121.1 24.6
##   .. .. .. ..@ ID       : chr &quot;5&quot;
##   .. .. .. ..@ area     : num 0.156
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 3
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.5 23.5
##   .. .. .. .. .. .. ..@ area   : num 1.11e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:33, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 24
##   .. .. .. .. .. .. ..@ area   : num 1.85e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:93, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.4 23.8
##   .. .. .. .. .. .. ..@ area   : num 0.416
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3135, 1:2] 122 122 122 122 122 ...
##   .. .. .. ..@ plotOrder: int [1:3] 3 2 1
##   .. .. .. ..@ labpt    : num [1:2] 121.4 23.8
##   .. .. .. ..@ ID       : chr &quot;6&quot;
##   .. .. .. ..@ area     : num 0.416
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 24.8
##   .. .. .. .. .. .. ..@ area   : num 0.000256
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:245, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.6 24.6
##   .. .. .. .. .. .. ..@ area   : num 0.229
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2200, 1:2] 122 122 122 122 122 ...
##   .. .. .. ..@ plotOrder: int [1:2] 2 1
##   .. .. .. ..@ labpt    : num [1:2] 121.6 24.6
##   .. .. .. ..@ ID       : chr &quot;7&quot;
##   .. .. .. ..@ area     : num 0.23
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.3 22.6
##   .. .. .. .. .. .. ..@ area   : num 0.000416
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:688, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121 23
##   .. .. .. .. .. .. ..@ area   : num 0.265
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2202, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:2] 2 1
##   .. .. .. ..@ labpt    : num [1:2] 121 23
##   .. .. .. ..@ ID       : chr &quot;8&quot;
##   .. .. .. ..@ area     : num 0.266
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.7 25.1
##   .. .. .. .. .. .. ..@ area   : num 0.00704
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:773, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.8 25.2
##   .. .. .. .. .. .. ..@ area   : num 2.08e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:63, 1:2] 122 122 122 122 122 ...
##   .. .. .. ..@ plotOrder: int [1:2] 1 2
##   .. .. .. ..@ labpt    : num [1:2] 121.7 25.1
##   .. .. .. ..@ ID       : chr &quot;9&quot;
##   .. .. .. ..@ area     : num 0.00706
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.9 24.5
##   .. .. .. .. .. .. ..@ area   : num 0.133
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1372, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 120.9 24.5
##   .. .. .. ..@ ID       : chr &quot;10&quot;
##   .. .. .. ..@ area     : num 0.133
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.9 23.8
##   .. .. .. .. .. .. ..@ area   : num 0.306
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:30, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 120.9 23.8
##   .. .. .. ..@ ID       : chr &quot;11&quot;
##   .. .. .. ..@ area     : num 0.306
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 61
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.4 23.2
##   .. .. .. .. .. .. ..@ area   : num 0.000699
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:509, 1:2] 119 119 119 119 119 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.2
##   .. .. .. .. .. .. ..@ area   : num 0.000102
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:158, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.3
##   .. .. .. .. .. .. ..@ area   : num 0.00018
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:236, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.3
##   .. .. .. .. .. .. ..@ area   : num 2.14e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:61, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.3
##   .. .. .. .. .. .. ..@ area   : num 7.56e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:162, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.3
##   .. .. .. .. .. .. ..@ area   : num 4.93e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:89, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.3
##   .. .. .. .. .. .. ..@ area   : num 6.93e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:25, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.3
##   .. .. .. .. .. .. ..@ area   : num 1.2e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:60, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.3 23.3
##   .. .. .. .. .. .. ..@ area   : num 2.03e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:70, 1:2] 119 119 119 119 119 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.3 23.3
##   .. .. .. .. .. .. ..@ area   : num 2.03e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:85, 1:2] 119 119 119 119 119 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.3
##   .. .. .. .. .. .. ..@ area   : num 5.72e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:32, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 1e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:34, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 8.47e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:41, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 0.000205
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:333, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 7.87e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:45, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 1.34e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:41, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.4
##   .. .. .. .. .. .. ..@ area   : num 0.000761
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:655, 1:2] 119 119 119 119 119 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.3 23.4
##   .. .. .. .. .. .. ..@ area   : num 0.00017
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:199, 1:2] 119 119 119 119 119 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.5
##   .. .. .. .. .. .. ..@ area   : num 0.000234
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:314, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.5
##   .. .. .. .. .. .. ..@ area   : num 4.53e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:106, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.5
##   .. .. .. .. .. .. ..@ area   : num 5.55e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:28, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.5
##   .. .. .. .. .. .. ..@ area   : num 3.5e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:22, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.5
##   .. .. .. .. .. .. ..@ area   : num 1.19e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:50, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.6
##   .. .. .. .. .. .. ..@ area   : num 9.04e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:35, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.6
##   .. .. .. .. .. .. ..@ area   : num 4.92e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:30, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.6
##   .. .. .. .. .. .. ..@ area   : num 3.12e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:37, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 5.11e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:31, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.6
##   .. .. .. .. .. .. ..@ area   : num 1.15e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:54, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 3.92e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:23, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 0.00654
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3794, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.6
##   .. .. .. .. .. .. ..@ area   : num 1.19e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:47, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 2.57e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:80, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 0.000179
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:197, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 9.27e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:63, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.6
##   .. .. .. .. .. .. ..@ area   : num 9.21e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:45, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.6
##   .. .. .. .. .. .. ..@ area   : num 4.98e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:36, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.6
##   .. .. .. .. .. .. ..@ area   : num 4.42e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:124, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 1.46e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:90, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 4.78e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:119, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 7.99e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:39, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 4.1e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:21, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 7.19e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:46, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.5 23.6
##   .. .. .. .. .. .. ..@ area   : num 0.0035
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:3107, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 2.03e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:84, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 1.79e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:63, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 3.32e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:20, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.7 23.7
##   .. .. .. .. .. .. ..@ area   : num 9.97e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:49, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 4.93e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:26, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 6.28e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:34, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 1.85e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:79, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 5.04e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:42, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 5.19e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:36, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 1.62e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:67, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 4.92e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:41, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 5e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:165, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 3.9e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:28, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.8
##   .. .. .. .. .. .. ..@ area   : num 6.14e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:45, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.8
##   .. .. .. .. .. .. ..@ area   : num 4.56e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:28, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.7
##   .. .. .. .. .. .. ..@ area   : num 0.000358
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:414, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.8
##   .. .. .. .. .. .. ..@ area   : num 2.22e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:87, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 119.6 23.8
##   .. .. .. .. .. .. ..@ area   : num 7.83e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:36, 1:2] 120 120 120 120 120 ...
##   .. .. .. ..@ plotOrder: int [1:61] 30 43 17 1 59 19 14 3 33 18 ...
##   .. .. .. ..@ labpt    : num [1:2] 119.6 23.6
##   .. .. .. ..@ ID       : chr &quot;12&quot;
##   .. .. .. ..@ area     : num 0.0137
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.4 22.3
##   .. .. .. .. .. .. ..@ area   : num 0.000637
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:350, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.7 22.5
##   .. .. .. .. .. .. ..@ area   : num 0.259
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4621, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:2] 2 1
##   .. .. .. ..@ labpt    : num [1:2] 120.7 22.5
##   .. .. .. ..@ ID       : chr &quot;13&quot;
##   .. .. .. ..@ area     : num 0.26
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.7 24.2
##   .. .. .. .. .. .. ..@ area   : num 0.0149
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:12, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 120.7 24.2
##   .. .. .. ..@ ID       : chr &quot;14&quot;
##   .. .. .. ..@ area     : num 0.0149
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.9 24.2
##   .. .. .. .. .. .. ..@ area   : num 0.235
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:2138, 1:2] 121 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.7 24.2
##   .. .. .. .. .. .. ..@ area   : num 0.0149
##   .. .. .. .. .. .. ..@ hole   : logi TRUE
##   .. .. .. .. .. .. ..@ ringDir: int -1
##   .. .. .. .. .. .. ..@ coords : num [1:12, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:2] 1 2
##   .. .. .. ..@ labpt    : num [1:2] 120.9 24.2
##   .. .. .. ..@ ID       : chr &quot;15&quot;
##   .. .. .. ..@ area     : num 0.235
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120 23
##   .. .. .. .. .. .. ..@ area   : num 0.013
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1333, 1:2] 120 120 120 120 120 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 120 23
##   .. .. .. ..@ ID       : chr &quot;16&quot;
##   .. .. .. ..@ area     : num 0.013
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 2
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 23.3
##   .. .. .. .. .. .. ..@ area   : num 1.86e-08
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 120.2 120.2 120.2 120.2 23.3 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.3 23.2
##   .. .. .. .. .. .. ..@ area   : num 0.136
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1703, 1:2] 120 120 120 120 120 ...
##   .. .. .. ..@ plotOrder: int [1:2] 2 1
##   .. .. .. ..@ labpt    : num [1:2] 120.3 23.2
##   .. .. .. ..@ ID       : chr &quot;17&quot;
##   .. .. .. ..@ area     : num 0.136
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 4
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 25
##   .. .. .. .. .. .. ..@ area   : num 0.228
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4309, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 25
##   .. .. .. .. .. .. ..@ area   : num 0.0238
##   .. .. .. .. .. .. ..@ hole   : logi TRUE
##   .. .. .. .. .. .. ..@ ringDir: int -1
##   .. .. .. .. .. .. ..@ coords : num [1:14, 1:2] 122 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122.1 25.5
##   .. .. .. .. .. .. ..@ area   : num 2.63e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:49, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122.1 25.6
##   .. .. .. .. .. .. ..@ area   : num 0.000121
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:101, 1:2] 122 122 122 122 122 ...
##   .. .. .. ..@ plotOrder: int [1:4] 1 2 4 3
##   .. .. .. ..@ labpt    : num [1:2] 122 25
##   .. .. .. ..@ ID       : chr &quot;18&quot;
##   .. .. .. ..@ area     : num 0.228
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 7
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.6 21.9
##   .. .. .. .. .. .. ..@ area   : num 0.000103
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:107, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.6 22.1
##   .. .. .. .. .. .. ..@ area   : num 1.23e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:45, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 122 22
##   .. .. .. .. .. .. ..@ area   : num 0.00419
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1013, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.5 22.7
##   .. .. .. .. .. .. ..@ area   : num 0.00148
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:537, 1:2] 121 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.5 22.7
##   .. .. .. .. .. .. ..@ area   : num 8.94e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:31, 1:2] 122 122 122 122 122 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.4 23.1
##   .. .. .. .. .. .. ..@ area   : num 3.68e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:81, 1:2] 121 121 121 121 121 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121 22.9
##   .. .. .. .. .. .. ..@ area   : num 0.343
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:4188, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int [1:7] 7 3 4 1 6 2 5
##   .. .. .. ..@ labpt    : num [1:2] 121 22.9
##   .. .. .. ..@ ID       : chr &quot;19&quot;
##   .. .. .. ..@ area     : num 0.349
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 1
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 121.2 24.9
##   .. .. .. .. .. .. ..@ area   : num 0.096
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1108, 1:2] 121 121 121 121 121 ...
##   .. .. .. ..@ plotOrder: int 1
##   .. .. .. ..@ labpt    : num [1:2] 121.2 24.9
##   .. .. .. ..@ ID       : chr &quot;20&quot;
##   .. .. .. ..@ area     : num 0.096
##   .. ..$ :Formal class &#39;Polygons&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. ..@ Polygons :List of 3
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.1 23.6
##   .. .. .. .. .. .. ..@ area   : num 3.24e-06
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:23, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.2 23.7
##   .. .. .. .. .. .. ..@ area   : num 1.54e-05
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:53, 1:2] 120 120 120 120 120 ...
##   .. .. .. .. ..$ :Formal class &#39;Polygon&#39; [package &quot;sp&quot;] with 5 slots
##   .. .. .. .. .. .. ..@ labpt  : num [1:2] 120.4 23.7
##   .. .. .. .. .. .. ..@ area   : num 0.101
##   .. .. .. .. .. .. ..@ hole   : logi FALSE
##   .. .. .. .. .. .. ..@ ringDir: int 1
##   .. .. .. .. .. .. ..@ coords : num [1:1162, 1:2] 120 120 120 120 121 ...
##   .. .. .. ..@ plotOrder: int [1:3] 3 2 1
##   .. .. .. ..@ labpt    : num [1:2] 120.4 23.7
##   .. .. .. ..@ ID       : chr &quot;21&quot;
##   .. .. .. ..@ area     : num 0.101
##   ..@ plotOrder  : int [1:22] 7 20 12 9 14 16 8 19 6 5 ...
##   ..@ bbox       : num [1:2, 1:2] 116.7 20.7 122.1 25.6
##   .. ..- attr(*, &quot;dimnames&quot;)=List of 2
##   .. .. ..$ : chr [1:2] &quot;x&quot; &quot;y&quot;
##   .. .. ..$ : chr [1:2] &quot;min&quot; &quot;max&quot;
##   ..@ proj4string:Formal class &#39;CRS&#39; [package &quot;sp&quot;] with 1 slots
##   .. .. ..@ projargs: chr NA
</code></pre>

<pre><code>## NULL
</code></pre>

<pre><code class="r">
taiwan_map &lt;- fortify(taiwan_shp)
</code></pre>

<pre><code>## Regions defined for each Polygons
</code></pre>

<pre><code class="r">head(taiwan_map)
</code></pre>

<pre><code>##    long   lat order  hole piece group id
## 1 120.2 22.75     1 FALSE     1   0.1  0
## 2 120.3 22.74     2 FALSE     1   0.1  0
## 3 120.3 22.71     3 FALSE     1   0.1  0
## 4 120.3 22.65     4 FALSE     1   0.1  0
## 5 120.3 22.61     5 FALSE     1   0.1  0
## 6 120.3 22.60     6 FALSE     1   0.1  0
</code></pre>

<pre><code class="r">
# Send the SpatialPolygonsDataFrame directly to ggplot()
ggplot(taiwan_shp, aes(x = long, y = lat, group = group)) + geom_path()
</code></pre>

<pre><code>## Regions defined for each Polygons
</code></pre>

<p><img src="" alt="plot of chunk unnamed-chunk-20"/> </p>

</body>

</html>

